package io.gitee.wiqer.medium;

import io.gitee.wiqer.TreeNode;

import java.util.ArrayList;

public class SolutionNC45_ThreeOrders {
    private  ArrayList<Integer> first = new ArrayList<>();
    private  ArrayList<Integer> middle = new ArrayList<>();
    private  ArrayList<Integer> then = new ArrayList<>();
    private  void firstOrder(TreeNode root){
        if(root != null){
            first.add(root.val);
            firstOrder(root.left);
            firstOrder(root.right);
        }
    }
    private  void middleOrder(TreeNode root){
        if(root != null){
            middleOrder(root.left);
            middle.add(root.val);
            middleOrder(root.right);
        }
    }
    private  void thenOrder(TreeNode root){
        if(root != null){
            thenOrder(root.left);
            thenOrder(root.right);
            then.add(root.val);
        }
    }
    private static final int[] toIntArray(ArrayList<Integer> arrayList){
        int[] intArray = arrayList.stream().mapToInt(Integer::intValue).toArray();
        return intArray;
    }
    /**
     *
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    public int[][] threeOrders (TreeNode root) {
        // write code here
        int[][] orders = new int[3][];
        firstOrder(root);
        orders[0] = toIntArray(first);
        middleOrder(root);
        orders[1] = toIntArray(middle);
        thenOrder(root);
        orders[2] = toIntArray(then);
        return orders;
    }
}
